﻿using System;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Collections.Generic;
using HsTec.DigitalPlatform.DataAccess.Search;


namespace HsTec.DigitalPlatform.DataAccess
{
    public class PlayerInfoRepository
    {
        DigitalPlatformEntities db = new DigitalPlatformEntities();

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="playerinfo"></param>
        public void Add(PlayerInfo playerinfo)
        {
            db.AddToPlayerInfos(playerinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="playerinfo"></param>
        public void Delete(PlayerInfo playerinfo)
        {
            db.DeleteObject(playerinfo);
            db.SaveChanges();
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            db.SaveChanges();
        }

        /// <summary>
        /// 根据ID查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PlayerInfo GetPlayerInfoByID(int id)
        {
            return db.PlayerInfos.FirstOrDefault<PlayerInfo>(d => d.playerid == id);
        }

        /// <summary>
        /// 返回所有实体
        /// </summary>
        /// <returns></returns>
        public IQueryable<PlayerInfo> FindALLPlayerInfo()
        {
            return db.PlayerInfos;
        }

        public IQueryable<ViPlayerInfo> PageInfo(int page, int rp, string sortname, string sortorder, PlayerSearchModel pageWhere)
        {
            var q = db.ViPlayerInfos.OrderBy("it." + sortname + " " + sortorder);
            return SearchInfo(q, pageWhere);
        }

        private IQueryable<ViPlayerInfo> SearchInfo(IQueryable<ViPlayerInfo> q, PlayerSearchModel pageWhere)
        {
            if (!String.IsNullOrEmpty(pageWhere.sname))
            {
                q = q.Where(t => t.playername.Contains(pageWhere.sname));
            }
            if (!String.IsNullOrEmpty(pageWhere.sno))
            {
                q = q.Where(t => t.playeno.Contains(pageWhere.sno));
            }
            if (!String.IsNullOrEmpty(pageWhere.sgrade))
            {
                q = q.Where(t => t.grade.Contains(pageWhere.sgrade));
            }
            if (!String.IsNullOrEmpty(pageWhere.spec))
            {
                q = q.Where(t => t.spename.Contains(pageWhere.spec));
            }
            if (!String.IsNullOrEmpty(pageWhere.sclass))
            {
                q = q.Where(t => t.classname.Contains(pageWhere.sclass));
            }
            return q;
        }

        public int GetAllCount(PlayerSearchModel pageWhere)
        {
            return SearchInfo(db.ViPlayerInfos.AsQueryable<ViPlayerInfo>(), pageWhere).Count();
        }

        public int GetMaxID()
        {
            return db.PlayerInfos.Max(p => p.playerid);
        }

        public bool Exist(string playerno)
        {
            return db.PlayerInfos.Where(t => t.playeno == playerno).Count() > 0 ? true : false;
        }

        public bool Exist(string playerno, string password)
        {
            return db.PlayerInfos.Where(t => t.playeno == playerno && t.password == password).Count() > 0 ? true : false;
        }

        public ViPlayerInfo GetPlayerInfoByPassword(string playerno, string password)
        {
            return db.ViPlayerInfos.FirstOrDefault<ViPlayerInfo>(t => t.playeno == playerno && t.password == password);
        }

        public string GenerationNo(string sortorder, string sortname)
        {
            List<int> listid = (from c in db.ViPlayerInfos
                                   orderby c.classname
                                   select c.playerid).ToList<int>();
            int iCount = 1;
            string No = String.Format("{0:D4}", iCount);
            PlayerInfo pi = new PlayerInfo();
            foreach (int pid in listid)
            {
                pi = this.GetPlayerInfoByID(pid);
                pi.playnumber = No;
                db.SaveChanges();
                No = String.Format("{0:D4}", ++iCount);
            }
            return "1";
        }
    }
}
